package org.jeecg.modules.yfFenfgl.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.workflow.utils.ProcessKit;
import org.jeecg.modules.yfFenfgl.entity.HqYfFenfgl;
import org.jeecg.modules.yfFenfgl.service.IHqYfFenfglService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.zhaojd.entity.HqZhaojd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 分房管理
 * @Author: jeecg-boot
 * @Date: 2020-07-15
 * @Version: V1.0
 */
@Api(tags = "分房管理")
@RestController
@RequestMapping("/yfFenfgl/hqYfFenfgl")
@Slf4j
public class HqYfFenfglController extends JeecgController<HqYfFenfgl, IHqYfFenfglService> {
    @Autowired
    private IHqYfFenfglService hqYfFenfglService;

    @Autowired
    private ISysDepartService sysDepartService;


    @GetMapping(value = "/getJunrxxxx")
    public Result<?> getJunrxxxx() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String id = sysDepartService.getByOrgCode(loginUser.getOrgCode()).getId();
        loginUser.setId(null);
//        将所属单位的id赋值返回前端
        loginUser.setDepartIds(id);
        return Result.ok(loginUser);
    }

    @AutoLog(value = "任务审批")
    @ApiOperation(value = "任务审批", notes = "任务审批")
    @PostMapping(value = "/completetask")
    public Result<?> completeTask(@RequestBody Map params) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        hqYfFenfglService.complateTask(loginUser, (String) params.get("tasyiktxxkId"), (String) params.get("processInstanceId"),
                (Integer) params.get("auditStatus"), (String) params.get("auditMemo") ,(String) params.get("fuj") );
        return Result.ok("操作成功");
    }


    @GetMapping(value = "/myapplylist")
    public Result<?> myApplyList(HqYfFenfgl hqYfFenfglb,
                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                 HttpServletRequest req) {
       log.debug("传入参数："+hqYfFenfglb);
        Page<HqYfFenfgl> page = new Page<HqYfFenfgl>(pageNo, pageSize);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        IPage<HqYfFenfgl> iPage = hqYfFenfglService.myApplyList(page, user, hqYfFenfglb);
        iPage.getRecords().stream().forEach(item -> {
            // 前端显示完整层级的组织机构
            String fullName = this.dig_parentId(item.getSuosdw());
            if (fullName.length() > 1)
                fullName = fullName.substring(1);
            item.setSuosdw_fullName(fullName);
        });
        return Result.ok(iPage);
    }


    @GetMapping(value = "/mytodolist")
    public Result<?> myTodoList(HqYfFenfgl hqYfFenfgl,
                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                HttpServletRequest req) {
        Page<HqYfFenfgl> page = new Page<HqYfFenfgl>(pageNo, pageSize);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        IPage<HqYfFenfgl> iPage = hqYfFenfglService.myTodoTasks(page, user, hqYfFenfgl);

        iPage.getRecords().stream().forEach(item -> {
            String fullName = this.dig_parentId(item.getSuosdw());
            if (fullName.length() > 1)
                fullName = fullName.substring(1);
            item.setSuosdw_fullName(fullName);
        });
        return Result.ok(iPage);
    }

    /*我的已办*/
    @GetMapping(value = "/mydonelist")
    public Result<?> myDoneList(HqYfFenfgl hqYfFenfgl,
                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                HttpServletRequest req) {
        Page<HqYfFenfgl> page = new Page<HqYfFenfgl>(pageNo, pageSize);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        IPage<HqYfFenfgl> iPage = hqYfFenfglService.myDoneTasks(page, user, hqYfFenfgl);
        iPage.getRecords().stream().forEach(item -> {
            String fullName = this.dig_parentId(item.getSuosdw());
            if (fullName.length() > 1)
                fullName = fullName.substring(1);
            item.setSuosdw_fullName(fullName);
        });
        return Result.ok(page);
    }
//  总表

    /**
     * 分页列表查询
     *
     * @param hqYfFenfgl
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "归档表-分页列表查询")
    @ApiOperation(value = "归档表-分页列表查询", notes = "归档表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(HqYfFenfgl hqYfFenfgl,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {

        Page<HqYfFenfgl> page = new Page<>(pageNo, pageSize);
        IPage<HqYfFenfgl> guidan = hqYfFenfglService.guidan(page, hqYfFenfgl);
        guidan.getRecords().forEach(record -> {
            if (!record.getProcessStatus().equals(ProcessKit.FLOW_STATUS_END)) {
                record.setProcessStatus(ProcessKit.FLOW_STATUS_END);
                hqYfFenfglService.updateById(record);
            }
        });
        guidan.getRecords().stream().forEach(item -> {
            String fullName = this.dig_parentId(item.getSuosdw());
            if (fullName.length() > 1)
                fullName = fullName.substring(1);
            item.setSuosdw_fullName(fullName);
        });
        return Result.ok(guidan);
    }

    /**
     * 添加
     *
     * @param hqYfFenfgl
     * @return
     */
    @AutoLog(value = "分房管理-添加")
    @ApiOperation(value = "分房管理-添加", notes = "分房管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody HqYfFenfgl hqYfFenfgl) {
        hqYfFenfglService.save(hqYfFenfgl);
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        hqYfFenfglService.startFlow(hqYfFenfgl, loginUser);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param hqYfFenfgl
     * @return
     */
    @AutoLog(value = "分房管理-编辑")
    @ApiOperation(value = "分房管理-编辑", notes = "分房管理-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HqYfFenfgl hqYfFenfgl) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        log.debug("分房编辑");
        hqYfFenfglService.updateById(hqYfFenfgl);
        hqYfFenfglService.applyAgain(hqYfFenfgl, loginUser);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "分房管理-通过id删除")
    @ApiOperation(value = "分房管理-通过id删除", notes = "分房管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        hqYfFenfglService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "分房管理-批量删除")
    @ApiOperation(value = "分房管理-批量删除", notes = "分房管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hqYfFenfglService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "分房管理-通过id查询")
    @ApiOperation(value = "分房管理-通过id查询", notes = "分房管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        HqYfFenfgl hqYfFenfgl = hqYfFenfglService.getById(id);
        if (hqYfFenfgl == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(hqYfFenfgl);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param hqYfFenfgl
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HqYfFenfgl hqYfFenfgl) {
        /*防止hqYfFenfgl为null*/
        if (null == hqYfFenfgl){
            hqYfFenfgl = new HqYfFenfgl();
        }
        List<HqYfFenfgl> guidan = hqYfFenfglService.guidanExcel(hqYfFenfgl);
        return super.exportGuid(request, hqYfFenfgl, HqYfFenfgl.class, "分房管理",guidan);
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HqYfFenfgl.class);
    }


    String  dig_parentId(String parentId) {
        SysDepart sysDepart = sysDepartService.getById(parentId);
        // 不显示根节点
        if(sysDepart != null ) {
            return dig_parentId(sysDepart.getParentId()) + "-" + sysDepart.getDepartName();
        }
        return "";
    }

}
